const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgList: [
            // 'http://applet.youjia.top/uploads/image/20191218/527bc373f689307a796469e12a2997f8.jpg',
            // 'http://applet.youjia.top/uploads/image/20191218/527bc373f689307a796469e12a2997f8.jpg',
            // 'http://applet.youjia.top/uploads/image/20191218/527bc373f689307a796469e12a2997f8.jpg',
        ],
        order:{},

        query: {
            score: 5,
            content: '文字内容',
            baseImg: [],
            product_id: 9,
            order_id: 222
        },
        contentLength: 140,
        contentLastLength: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            'query.order_id': options.order_id,
            'query.product_id': options.product_id,
        })
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let _this = this;
        app.loading();
        app.HttpsRequest('user.order/detail', 'GET', {
            order_id: _this.data.query.order_id
        }).then(ret => {
            wx.hideLoading();
            if (ret) {
                _this.setData({
                    order: ret.data.detail,
                })
            }
        });
    },

    deleteImg(e) {
        let _this = this;
        let index = e.currentTarget.dataset.index;
        let images = _this.data.imgList;
        images.splice(index, 1);
        let baseImg = _this.data.query.baseImg;
        baseImg.splice(index, 1);
        _this.setData({
            imgList: images,
            'query.baseImg': baseImg,
        })
    },
    chooseImage() {
        let _this = this;
        app.loading();
        wx.chooseImage({
            number: 3,
            success(res) {
                const tempFilePaths = res.tempFilePaths;
                for (let key in tempFilePaths) {
                    wx.getFileSystemManager().readFile({
                        filePath: tempFilePaths[key],
                        encoding: 'base64', //编码格式
                        success: (res) => {
                            _this.setData({
                                'query.baseImg': _this.data.query.baseImg.concat('data:image/png;base64,' + res.data),
                                imgList: _this.data.imgList.concat(tempFilePaths[key])
                            });
                            wx.hideLoading();
                        }
                    });
                };
            }
        })
    },
    save() {
        let _this = this;
        let query = _this.data.query;
        console.log(query);
        if (query.baseImg.length == 0 && '' == query.content) {
            return app.tips('评论不为空');
        }
        app.loading();
        app.HttpsRequest('user.comment/create', 'POST', query).then(ret => {
            if (ret) {
                wx.hideLoading();
                wx.navigateBack();
            }
        })

    },
    ipt(event) {
        console.log(event);
        this.setData({
            'query.content': event.detail.value,
            contentLastLength: event.detail.value.length
        })
    },
    rateChange(event) {
        app.loading();
        this.setData({
            rate: event.detail
        });
        wx.hideLoading();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})